﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Data;
using System.Windows;
using System.Windows.Media.Effects;
using System.Windows.Media;

namespace TouchUmlUI
{
    public class MessageHint : Canvas
    {
        private String message;
        private double alpha = 1.0f;
        private System.Windows.Forms.Timer Clock;
        private int duration;
        private int alphaDurationStart;
        private int tickCount = 0;
        private double alphaDelta;
        private InkCanvas canvasParent;

        public String getMessage() {
            return message;
        }

        public void Timer_Tick(object sender, EventArgs eArgs)
        {
            tickCount += Clock.Interval;
            if (tickCount > alphaDurationStart)
            {
                this.Opacity = alpha;
                alpha -= alphaDelta;
                if (alpha <= 0)
                {
                    this.Visibility = Visibility.Hidden;
                    Clock.Stop();
                    canvasParent.Children.Remove(this);
                }
            }
        }

        public void setMessage(String message)
        {
            this.message = message;
        }

        public MessageHint(InkCanvas canvasParent,String message,int left, int top,System.Windows.Media.Brush bgBrush,int duration,int alphaDurationStart)
        {            
            this.canvasParent = canvasParent;
            this.duration = duration;
            this.alphaDurationStart = alphaDurationStart;
            alphaDelta = 10.0f / (double)(duration - alphaDurationStart);
            this.Background = bgBrush;
            InkCanvas.SetLeft(this, left);
            InkCanvas.SetTop(this, top);
            this.Opacity = 1.0f;
            setMessage(message);            
            TextBlock tb = new TextBlock(new Run(message));
            tb.FontFamily = new FontFamily("Calibri");
            tb.FontSize = 16;
            this.Children.Add(tb);
            Clock = new System.Windows.Forms.Timer();
            Clock.Interval = 10;
            Clock.Tick += new EventHandler(Timer_Tick);
            Clock.Start();
            this.Width = tb.ActualWidth;
            this.Height = tb.ActualHeight;
            Binding bind1 = new Binding("ActualWidthProperty");
            bind1.Source = tb;
            bind1.Path = new PropertyPath(TextBlock.ActualWidthProperty);
            this.SetBinding(Canvas.WidthProperty, bind1);
            Binding bind2 = new Binding("ActualHeightProperty");
            bind2.Source = tb;
            bind2.Path = new PropertyPath(TextBlock.ActualHeightProperty);
            this.SetBinding(Canvas.HeightProperty, bind2);
                
        }

    }
}
